package org.nhindirect.stagent;

import java.util.Collection;
import java.util.Collections;

import javax.mail.internet.InternetAddress;

import org.nhindirect.stagent.trust.TrustEnforcementStatus;

import java.security.cert.X509Certificate;

public class NHINDAddress extends InternetAddress
{
    X509Certificate m_certificate;
    TrustEnforcementStatus m_trustStatus;
    Collection<X509Certificate> m_trustAnchors;
            
    public NHINDAddress(String address)
    {
    	super();
    	setAddress(address);
    }

    public NHINDAddress(InternetAddress address)
    {
    	super();
    	setAddress(address.getAddress());
    }

    public NHINDAddress(String address, X509Certificate certificate)
    {            
    	super();
        setAddress(address);
    	this.m_certificate = certificate;
    }
    
    
    public String getHost()
    {
    	String retVal = "";
    	
    	int index = this.getAddress().indexOf("@");
    	if (index >= 0)
    		retVal = this.getAddress().substring(index + 1);
    	
    	return retVal;
    }
    
    public X509Certificate getCertificate()
    {
        return this.m_certificate;
    }
 
    public void setCertificate(X509Certificate value)
    {
        this.m_certificate = value;
    }

    
    public boolean hasCertificate()
    {
        return (this.m_certificate != null);
    }
    
    public Collection<X509Certificate> getTrustAnchors()
    {
    	return Collections.unmodifiableCollection(this.m_trustAnchors);
    }            
    
    public void setTrustAnchors(Collection<X509Certificate> value)
    {
        this.m_trustAnchors = value;
    }
    
    public boolean hasTrustAnchors()
    {
        return (this.m_trustAnchors != null && this.m_trustAnchors.size() > 0);
    }
    
    public TrustEnforcementStatus getStatus()
    {
        return this.m_trustStatus;
    }
        
    public void setStatus(TrustEnforcementStatus value)
    {
        this.m_trustStatus = value;
    }
           
    
    public boolean isTrusted(TrustEnforcementStatus minTrustStatus)
    {
        return (this.m_trustStatus.compareTo(minTrustStatus) >= 0);
    }
}
